package com.daona.job.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.daona.infrastructure.common.FilePathUtil;
import com.daona.infrastructure.config.MinioConfig;
import com.daona.infrastructure.minio.MinioUtils;
import com.daona.infrastructure.repository.db.mapper.MetricsMapper;
import com.daona.job.flow.context.PdfContext;
import com.daona.model.constants.Constants;
import com.daona.model.message.PdfMessage;
import com.daona.model.template.PdfModel;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
@Component
public class PdfJobFacade {

    private final PdfService pdfService;

    private final MinioUtils minioUtils;

    private final MinioConfig minioConfig;

    private final MetricsMapper metricsMapper;


    @Value("${daona.pdf.report}")
    private String reportFolder;

    @Resource
    private FlowExecutor flowExecutor;

    private PdfModel getPdfData(long id) {
        var metrics = metricsMapper.selectById(id);
        return PdfModel.builder()
                .estateTitle("物业行情报告")
                .estimate(metrics.getEstateMetric())
                .estimateRate(StrUtil.format("{}%", metrics.getEstateRate()*100))
                .autoTitle("汽车行情报告")
                .auto(metrics.getAutoMetirc())
                .autoRate(StrUtil.format("{}%", metrics.getAutoRate()*100))
                .financeTitle("金融行情报告")
                .finance(metrics.getFianaceMetric())
                .financeRate(StrUtil.format("{}%", metrics.getFinanceRate()*100))
                .build();
    }

    public void PdfJobProcess(PdfMessage message) {
        // 生成pdf地址
        String output = FilePathUtil.getRealFilePath(reportFolder + "/report.pdf");
        File file = FileUtil.file(output);

        var data = getPdfData(message.getMessageId());

        PdfContext context = PdfContext.builder()
                .pdfModel(data)
                .bucketName(minioConfig.getBucketName())
                .fileName(file.getName())
                .filePath(file.getPath())
                .output(output)
                .build();

        LiteflowResponse response = flowExecutor.execute2Resp(Constants.PDF_CHAIN, context, PdfContext.class);
        if(response.isSuccess()){
            log.info("pdf job execute successfully");
        }else {
            log.error("pdf job execute failed");
        }
    }

    public void JobProcess() {
        PdfModel data = PdfModel.builder()
                .build();
        try {
            String output = FilePathUtil.getRealFilePath(reportFolder + "/report.pdf");
            File file = FileUtil.file(output);
            List<String> pdfs = pdfService.generateMultiplePdf(data);
            pdfService.mergePdf(pdfs, output);
            minioUtils.uploadFile(minioConfig.getBucketName(), file.getName(), file.getPath());
            log.info("operate minio successfully");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
